<?php

namespace App\Controllers\Order;

use App\Models\Order\Order;
use App\Models\Order\PailebaoOrder;
use \App\Libs\Pay\PayLibs;

class AlipayRefund extends \App\Libs\Base\BaseAction
{

    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }

    public function execute()
    {
        $uid = $this->session->user->id;
        if ($this->request->has('order_sn') || $this->request->has('charge_sn')) {
            //处理买东西的退款
            if ($this->request->has('order_sn')) {
                $orderSn = $this->request->get('order_sn');
                //检查订单是否可以退款
                $OrderOrCode = Order::checkOrderRefund($uid, $orderSn);
                if (is_numeric($OrderOrCode)) {
                    switch ($OrderOrCode) {
                        case '-10':
                            return \App\Libs\Views\Json::view(1, '失败：订单不存在！', null);
                            break;
                        default:
                            return \App\Libs\Views\Json::view(1, "失败：订单状态" . Order::getOrderText($OrderOrCode) . "不允许退款!!", null);
                            break;
                    }
                }
                //更改状态，退款中；
                $OrderOrCode->status = '7';
                $OrderOrCode->update();
                sleep(10);
                //开始退款；
                $refundInfo = [
                    'out_trade_no' => $OrderOrCode->order_sn,       //本系统的订单号
                    'refund_amount' => $OrderOrCode->price,  //退款金额
                ];
                $alipay = new PayLibs(1);
                $response = $alipay->alipayRefund($refundInfo);
                if (!is_array($response)) {
                    //处理表中的逻辑;
                    $OrderOrCode->status = '6';  //6代表已退款
                    $backRefundInfo = json_encode($response);
                    $OrderOrCode->refund_info = $backRefundInfo;
                    $ticket_id = $OrderOrCode->ticket_id;
                    $updateRes = $OrderOrCode->update();
                    if (!$updateRes) {
                        //支付宝已经退款，改订单status失败;
                        //todo::记录日志
                    }
                    //todo::根据$ticket_id恢复购物券

                    return \App\Libs\Views\Json::view(0, '退款成功：' . $response->msg . '(alipay)', [
                        "refund_to" => $response->buyer_logon_id,
                        "refund_fee" => $response->refund_fee,
                    ]);
                } else {
                    return \App\Libs\Views\Json::view(1, '退款失败:' . $response['msg'] . '(alipay)', NULL);
                }
            } elseif ($this->request->has('charge_sn')) {
                $chargeSn = $this->request->get('charge_sn');
                $strOrPaileObj = PailebaoOrder::checkRefund($uid, $chargeSn);
                if (is_object($strOrPaileObj)) {
                    //信息显示：退款中
                    $strOrPaileObj->order_status = '7';   //3，退款中
                    $strOrPaileObj->update();
                    //发起退款流程
                    $refundInfo = [
                        'out_trade_no' => $strOrPaileObj->order_sn,       //本系统的订单号
                        'refund_amount' => $strOrPaileObj->money_num,  //退款金额
                    ];
                    $alipay = new PayLibs(1);
                    $response = $alipay->alipayRefund($refundInfo);
                    if (!is_array($response)) {
                        //处理表中的逻辑;
                        $strOrPaileObj->order_status = '6';  //2代表退款成功
                        $backrefundInfo = json_encode($response);
                        $strOrPaileObj->refund_info = $backrefundInfo;
                        $updateRes = $strOrPaileObj->update();
                        if (!$updateRes) {
                            //支付宝已经退款，改订单status失败;
                            //todo::记录日志,人工排查bug
                        }
                        return \App\Libs\Views\Json::view(0, '退款成功：' . $response->msg . '(alipay)', [
                            "refund_to" => $response->buyer_logon_id,
                            "refund_fee" => $response->refund_fee,
                        ]);
                    } else {
                        return \App\Libs\Views\Json::view(1, '退款失败:' . $response['msg'] . '(alipay)', NULL);
                    }
                } elseif ($strOrPaileObj == (-10)) {
                    return \App\Libs\Views\Json::view(1, '退款失败:订单不存在！', NULL);
                } else {
                    return \App\Libs\Views\Json::view(1, '退款失败:' . $strOrPaileObj, NULL);
                }
            } else {
                return \App\Libs\Views\Json::view(1, '参数不正确!', NULL);
            }
        } else {
            return \App\Libs\Views\Json::view(1, '缺少：订单号！', NULL);
        }
    }
}
